package com.pig4cloud.pig.homecenter.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class CustomHassDateDeserializer extends JsonDeserializer<LocalDateTime> {

	private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	@Override
	public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
		if(p.currentToken().isNumeric()){
			Long timestamp = p.getDecimalValue().longValue();
			// 将秒级时间戳转换为Instant
			Instant instant = Instant.ofEpochSecond(timestamp);

			// 指定时区，例如使用系统默认时区
			ZoneId zoneId = ZoneId.systemDefault();

			// 将Instant转换为ZonedDateTime
			ZonedDateTime zonedDateTime = instant.atZone(zoneId);
			return zonedDateTime.toLocalDateTime();
		}else{

			return LocalDateTime.parse(p.getText(),formatter);
		}
	}
}